/**
 * @Author: 罗显
 * @CreateTime: 2025-04-30
 * @Version: 1.0
 */


package com.it.user.mapper;

import com.it.user.entity.UserEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    //登录
    @Select("select * from `wm_user` where name=#{name} and password=#{password}")
    UserEntity login(@Param("name") String username, @Param("password") String password);

    //根据用户名查询用户信息 检查用户是否存在
    @Select("select * from `wm_user` where name=#{name}")
    UserEntity findUserByUsername(@Param("name") String username);

    //注册
    @Insert("insert into `wm_user` (name,password,nickname,image,email,phone,created_time,updated_time) values(#{name},#{password},#{nickname},#{image},#{email},#{phone},#{createTime},#{updateTime})")
    void register(UserEntity userEntity);

    //更新用户状态
    @Update("update `wm_user` set status=#{status} where id=#{id}")
    void updateStatus(@Param("id") Long id, @Param("status") int status);
    //根据id查询用户信息
    @Select("select * from `wm_user` where id=#{id}")
    UserEntity findUserById(@Param("id") Long id);

    //查询用户信息 单个用户进行查询 用户登录成功后将进行的查询 然后根据用户的信息 由用户进行修改用户信息  修改手机号 邮箱 昵称 头像  头像由用户上传图片  通过id锁定用户信息 不需要返回用户信息
    @Update("<script>" +
            "update `wm_user` " +
            "<set>" +
            "  <if test='phone != null and phone != \"\"'>phone = #{phone},</if>" +  //同时判断是否为null和空字符串
            "  <if test='email != null and email != \"\"'>email = #{email},</if>" +
            "  <if test='nickname != null and nickname != \"\"'>nickname = #{nickname},</if>" +
            "  <if test='image != null and image != \"\"'>image = #{image},</if>" +
            "</set>" +
            "where id = #{id}" +
            "</script>")
    int updateUserById(UserEntity userEntity);

    //用户修改密码
    @Update("update `wm_user` set password=#{password} where id=#{id}")
    int updatePassword(@Param("id") Long id, @Param("password") String password);

}